package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysActionDAO;
import com.ourteam.system.dao.SysAction;
import com.ourteam.system.dao.SysActionDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:36
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysActionDAOJdbcImpl extends AbstractSpringDAO
    implements ISysActionDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ModuleId, ActionCode, ActionName, DomainId, Remarks, Status,
            ActionResource, DomainCode, DomainName, ModuleCode, ModuleName
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ModuleId,
            _DataObjectProperty.ActionCode, _DataObjectProperty.ActionName,
            _DataObjectProperty.DomainId, _DataObjectProperty.Remarks,
            _DataObjectProperty.Status, _DataObjectProperty.ActionResource,
            _DataObjectProperty.DomainCode, _DataObjectProperty.DomainName,
            _DataObjectProperty.ModuleCode, _DataObjectProperty.ModuleName
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] { ActionCode };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysAction, SysActionDAOQueryBean> daoHelper;
    private com.ourteam.system.dao.impl.AbstractSysRoleActionRelateDAOJdbcImpl SysRoleActionRelateDao;

    public AbstractSysActionDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysAction, SysActionDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysDomainDAO.EntityName)
                                .append(" sysDomainA ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysDomainDAO.Id,
                    com.ourteam.system.dao.ISysDomainDAO.EntityName + ".",
                    "sysDomainA."));
            relatedJoinSQLBuffer.append(" = ").append(DomainId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysModuleDAO.EntityName)
                                .append(" sysModuleA ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysModuleDAO.Id,
                    com.ourteam.system.dao.ISysModuleDAO.EntityName + ".",
                    "sysModuleA."));
            relatedJoinSQLBuffer.append(" = ").append(ModuleId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysAction
         * @throws Exception
         */
    public boolean checkIsUnique(SysAction aSysAction)
        throws Exception {
        if (aSysAction == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysAction.getId());

        valueList.add(aSysAction.getActionCode());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysAction
     * @throws Exception
     */
    public void insert(SysAction aSysAction) throws Exception {
        super.doInsert(aSysAction);
    }

    /**
         * Batch Insert Datas
         * @param aSysAction
         * @throws Exception
         */
    public void batchInsert(SysAction[] aSysActions) throws Exception {
        super.doBatchInsert(aSysActions);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysActionDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(SysAction aSysAction) throws Exception {
        delete(aSysAction.getId());
    }

    /**
     * Update Data Selective
     * @param aSysAction
     * @throws Exception
     */
    public void update(SysAction aSysAction) throws Exception {
        if (aSysAction.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysAction);

        SysAction tempObj = findById(aSysAction.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysAction, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysActions[]
     * @throws Exception
     */
    public void batchUpdate(SysAction[] aSysActions) throws Exception {
        super.doBatchUpdate(aSysActions);
    }

    /**
         * Update Data By Condictions
         * @param aSysAction
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysAction aSysAction, SysActionDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aSysAction, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysActionDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(SysAction aSysAction)
        throws Exception {
        if (aSysAction.getActionResource() == null) {
            throw new Exception(
                "The SysAction.actionResource value is not allow NULL");
        }

        if (aSysAction.getActionCode() == null) {
            throw new Exception(
                "The SysAction.actionCode value is not allow NULL");
        }

        if (aSysAction.getActionName() == null) {
            throw new Exception(
                "The SysAction.actionName value is not allow NULL");
        }

        if (aSysAction.getRemarks() == null) {
            throw new Exception("The SysAction.remarks value is not allow NULL");
        }

        if (aSysAction.getStatus() == null) {
            throw new Exception("The SysAction.status value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysAction findById(final long id) throws Exception {
        SysActionDAOQueryBean queryBean = new SysActionDAOQueryBean();

        SysActionDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysAction[] SysActions = query(queryBean);

        if (SysActions.length == 0) {
            return null;
        } else {
            return SysActions[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysAction[]
         * @throws Exception
         */
    public SysAction[] query(SysActionDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean, new JdbcSysActionRowMapper());

        return (SysAction[]) result.toArray(new SysAction[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(SysActionDAOQueryBean queryBean)
        throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysActionDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(SysAction[] aSysActions,
        SysActionDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aSysActions, queryBean);
    }

    public void synchronize(SysAction[] aSysActions,
        SysActionDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysAction> callBack)
        throws Exception {
        daoHelper.synchronize(aSysActions, queryBean, callBack);
    }

    public void setSysRoleActionRelateDao(
        com.ourteam.system.dao.ISysRoleActionRelateDAO SysRoleActionRelateDao) {
        this.SysRoleActionRelateDao = (com.ourteam.system.dao.impl.AbstractSysRoleActionRelateDAOJdbcImpl) SysRoleActionRelateDao;
    }

    /**
     * Query Related SysRoleActionRelate
    * @param queryBean
     * @throws Exception
     */
    public com.ourteam.system.dao.SysRoleActionRelate[] querySysRoleActionRelate(
        SysActionDAOQueryBean queryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysRoleActionRelateDAO.ActionId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysRoleActionRelateDAO.EntityName +
            " ref_SysRoleActionRelate";

        String[] entityFields = com.ourteam.system.dao.impl.SysRoleActionRelateDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysRoleActionRelateDAO.ENTITY_PROPERTIES;

        List list = SysRoleActionRelateDao.doQuery(entityName, entityFields,
                entityProperties, queryBean, joinSQL.toString(),
                new com.ourteam.system.dao.impl.SysRoleActionRelateDAO.JdbcSysRoleActionRelateRowMapper());

        return (com.ourteam.system.dao.SysRoleActionRelate[]) list.toArray(new com.ourteam.system.dao.SysRoleActionRelate[list.size()]);
    }

    /**
     * Query Related SysRoleActionRelate Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySysRoleActionRelateSelective(
        SysActionDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleActionRelateDAO.Id,
                "id");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleActionRelateDAO.ActionId,
                "actionId");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleActionRelateDAO.Remarks,
                "remarks");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleActionRelateDAO.RoleId,
                "roleId");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleActionRelateDAO.Status,
                "status");
        }

        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysRoleActionRelateDAO.ActionId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysRoleActionRelateDAO.EntityName +
            " ref_SysRoleActionRelate";

        return SysRoleActionRelateDao.doQuerySelective(entityName, queryBean,
            joinSQL.toString());
    }

    /**
     * Update Related SysRoleActionRelate
     * @param aSysRoleActionRelate
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void updateSysRoleActionRelate(
        com.ourteam.system.dao.SysRoleActionRelate aSysRoleActionRelate,
        SysActionDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysRoleActionRelateDAO.ActionId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysRoleActionRelateDAO.EntityName +
            " ref_SysRoleActionRelate";

        String[] entityFields = com.ourteam.system.dao.impl.SysRoleActionRelateDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysRoleActionRelateDAO.ENTITY_PROPERTIES;

        SysRoleActionRelateDao.doUpdateByQuery(entityName, entityFields,
            entityProperties, joinSQL.toString(), aSysRoleActionRelate,
            aQueryBean);
    }

    /**
     * Delete Related SysRoleActionRelate
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void deleteSysRoleActionRelate(SysActionDAOQueryBean aQueryBean)
        throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysRoleActionRelateDAO.ActionId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysRoleActionRelateDAO.EntityName; // + " ref_SysRoleActionRelate";

        SysRoleActionRelateDao.doDeleteByQuery(entityName, joinSQL.toString(),
            aQueryBean);
    }

    public static class JdbcSysActionRowMapper implements RowMapper<SysAction> {
        public SysAction mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysAction object = new SysAction();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object moduleId = null;

                moduleId = resultSet.getObject(columnIndex++);

                object.setModuleIdNull(resultSet.getBoolean(columnIndex++));

                if (moduleId != null) {
                    setBeanValue(object, _DataObjectProperty.ModuleId, moduleId);
                }

                Object actionCode = null;

                actionCode = resultSet.getObject(columnIndex++);

                object.setActionCodeNull(resultSet.getBoolean(columnIndex++));

                if (actionCode != null) {
                    setBeanValue(object, _DataObjectProperty.ActionCode,
                        actionCode);
                }

                Object actionName = null;

                actionName = resultSet.getObject(columnIndex++);

                object.setActionNameNull(resultSet.getBoolean(columnIndex++));

                if (actionName != null) {
                    setBeanValue(object, _DataObjectProperty.ActionName,
                        actionName);
                }

                Object domainId = null;

                domainId = resultSet.getObject(columnIndex++);

                object.setDomainIdNull(resultSet.getBoolean(columnIndex++));

                if (domainId != null) {
                    setBeanValue(object, _DataObjectProperty.DomainId, domainId);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object actionResource = null;

                actionResource = resultSet.getObject(columnIndex++);

                object.setActionResourceNull(resultSet.getBoolean(columnIndex++));

                if (actionResource != null) {
                    setBeanValue(object, _DataObjectProperty.ActionResource,
                        actionResource);
                }

                Object domainCode = null;

                domainCode = resultSet.getObject(columnIndex++);

                object.setDomainCodeNull(resultSet.getBoolean(columnIndex++));

                if (domainCode != null) {
                    setBeanValue(object, _DataObjectProperty.DomainCode,
                        domainCode);
                }

                Object domainName = null;

                domainName = resultSet.getObject(columnIndex++);

                object.setDomainNameNull(resultSet.getBoolean(columnIndex++));

                if (domainName != null) {
                    setBeanValue(object, _DataObjectProperty.DomainName,
                        domainName);
                }

                Object moduleCode = null;

                moduleCode = resultSet.getObject(columnIndex++);

                object.setModuleCodeNull(resultSet.getBoolean(columnIndex++));

                if (moduleCode != null) {
                    setBeanValue(object, _DataObjectProperty.ModuleCode,
                        moduleCode);
                }

                Object moduleName = null;

                moduleName = resultSet.getObject(columnIndex++);

                object.setModuleNameNull(resultSet.getBoolean(columnIndex++));

                if (moduleName != null) {
                    setBeanValue(object, _DataObjectProperty.ModuleName,
                        moduleName);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
